var content = require('../../../public/content');
var select = require('{pub}/select');
var numeral = require('{plus}/numeral');
var checkradio = require('{plus}/checkradio');
var tpl = require('../../../views/initialize/setting');
var template = require('template');
var request = require('request');
var calendar = require('{plus}/calendar');
var validate = require('{plus}/validate');

return extend(content, {
    target:'#system-setting',
    path:'/system-setting/',
    template:tpl,
    data:{
        title:'系统设置',
        size:'4,2,2,2'
    },
    events:{
        'focus [data-integer]':function(e, elem){
            elem.select()
        },
        'click .ui-button':function(){
            this.element.find('form').submit()
        },
        'click [name="date"]':function(){
            $.calendar({
                format:'yyyy年MM月',
                ismonth:true,
                monthcount:4,
                button:[{
                    name:'close',
                    text:'取消'
                },{
                    name:'confirm',
                    text:'确定'
                }]
            })
        }
    },
    initCom:function(){
        var that = this;
        this.element.find('[name="accounting"]').imitSelect();
        this.element.find('[name="level"]').imitSelect({
            count:4,
            skin:'drowdown',
            button:{
                enable:true,
                prev:'<s class="iconfont">&#xe65b;</s>',
                next:'<s class="iconfont">&#xe636;</s>'
            },
            callback:function(ele, item){
                if(item){
                    var level = ele.val();
                    ele.closest('.inner').removeClass().addClass('inner level'+level);
                    var elem = that.element.find('.level-wrap');
                    var length = elem.find('.ui-input-scale').length;
                    var result = level - length;
                    if(result > 0){
                        that.initLevel($(template.render.call(tpl, tpl.levels, new Array(result))).appendTo(elem));
                    }
                    else if(result < 0){
                        result *= -1;
                        while(result){
                            elem.find('.ui-input-scale').eq(-1).prev().addBack().remove();
                            result--
                        }
                    }
                }
            }
        });

        this.element.find(':checkbox, :radio').checkradio({
            callback:function(ele){
                if(ele.attr('name') === 'verify'){
                    var checkbox = ele.closest('.inner').find(':checkbox');
                    checkbox.checkradio('disabled', ele.val() !== '2')
                }
            }
        });

        this.initLevel();
    },
    initLevel:function(elem){
        (elem||this.element).find('[data-integer]').numeral({
            callback:function(elem, val){
                var min = elem.data('min');
                if(min && val < min){
                    elem.val(min).select()
                }
            }
        })
    },
    onInit:function(){
        this.initCom();
        this.request();
    },
    request:function(){
        var that = this;
        request.get(sourceHttp+'?sleep=1', {}, {
            200:function(res){
                that.render(res.data);
                that.validate();
            }
        }, '正在初始化系统设置...')
    },
    validate:function(){
        this.element.find('form').validate({
            
        })
    }
})